/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.LisPatient;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/otherInfo")
public class LisToOtherCtrl extends Controller {

    private Gson gson = new Gson();

    private LisPatient lispat = new LisPatient();

    public void index() {

    }

    //接收其他医院的检查信息
    @Before(Tx.class)
    public void recviceLisInfo() {
        Map<String, String> info = gson.fromJson(getPara("lisinfo"), new TypeToken<Map<String, String>>() {
        }.getType());
        System.out.println(info);
        String infopk = lispat.saveOtherInfo(info);
        try {
            Record sampleInfo = new Record();
            sampleInfo.set("INFOPK", infopk);
            sampleInfo.set("BARCODE", info.get("BARCODE"));
            sampleInfo.set("SAMPLE", info.get("SAMPLE"));
            sampleInfo.set("CYSJ", info.get("CYSJ"));
            sampleInfo.set("STAYHOSPITALMODE", info.get("STAYHOSPITALMODE"));
            sampleInfo.set("EXAMINAIM", info.get("EXAMINAIM"));
            sampleInfo.set("BED_NO", info.get("BED_NO"));
            sampleInfo.set("YBLB", "1");
            sampleInfo.set("HOSPITAL_CODE", getPara("hospital_code"));
            boolean flag = Db.save("lis_samplecollection", sampleInfo);
            System.out.println(flag);
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }

    }

    public void recviceData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Record> list_re = Db.find("select * from lis_samplecollection a left join lis_patient b on a.INFOPK=b.pk where HOSPITAL_CODE='" + getPara("hospital_code") + "' and RETURN_RESULT_FLAG=2 and SEND_CENTER_STATUS<2 limit 10");

        //System.out.println("select * from lis_samplecollection where HOSPITAL_CODE='" + getPara("hospital_code") + "' and RETURN_RESULT_FLAG=2 and SEND_CENTER_STATUS<2 limit 10");
        // System.out.println(list_re.size());
        if (list_re != null) {
            for (Record re : list_re) {

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("PATIENTNAME", re.get("PATIENTNAME"));
                map.put("AGE", re.get("AGE"));
                map.put("SEX", re.get("SEX"));
                map.put("BARCODE", re.get("BARCODE"));
                map.put("EXAMINAIM", re.get("EXAMINAIM"));
                map.put("CHECKOPERATOR", re.get("CHECKOPERATOR"));
                map.put("CHECKTIME", re.get("CHECKTIME"));
                map.put("JYYS", re.get("JYYS"));
                map.put("SAMPLENO", re.get("SAMPLENO"));
                map.put("UUID", re.get("UUID"));
                map.put("result", Db.find("select * from lis_result where uuid='" + re.get("UUID") + "'"));

                list.add(map);
            }

        }
        if (list != null && !list.isEmpty()) {

            renderJson(list);
        } else {
            renderText("false");
        }

    }

    public void updateDataStatus() {
        try {
            String brcodes[] = getPara("brcodes").split(",");
            for (String brcode : brcodes) {
                Db.update("update lis_samplecollection set SEND_CENTER_STATUS=2 where barcode='" + brcode + "'");
                renderText("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }

    }
}
